package com.mcptt.config.body.location;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.models.lbs.LocationData;

public class LocationInfoBody_Report {


	private static final String THIS_FILE = "LocationInfoBody_New";
	private static final String End_Mcptt_Tag = "</location-info>";
	private static final String Name_TriggerId = "TriggerId";
	private static final String Name_Latitude = "Latitude";
	private static final String Name_Longitude = "Longitude";
	
	private String mcpttId = null;
	private boolean isLocTrigger = false;
	private LocationData data = null;
	
	private LocationInfoBody_Report()
	{
		this.mcpttId = "";
		this.isLocTrigger = false;
		this.data = new LocationData();
	}
	public LocationInfoBody_Report(String mcpttId,boolean isLocTrigger,LocationData data)
	{
		this.mcpttId = mcpttId;
		this.isLocTrigger = isLocTrigger;
		this.data = data;
	}
	
	public static LocationInfoBody_Report parseXmlStr(String xmlStr) throws Exception 
	{    
		LocationInfoBody_Report body = new LocationInfoBody_Report();
		
	    int index = xmlStr.indexOf(End_Mcptt_Tag);
//	    Log.d(THIS_FILE, "McpttInfoBody_NewCall parseXmlStr index = " + index);
	    
//	    if(index <= 0)
//	    {
//	    	key = End_Mcvideo_Tag;
//	    	index = xmlStr.indexOf(key);
//	    }
	    xmlStr= xmlStr.substring(0, index + End_Mcptt_Tag.length());
	    xmlStr = xmlStr.trim();

		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据        
		int event = pullParser.getEventType();

		while (event != XmlPullParser.END_DOCUMENT){

			switch (event) {

			case XmlPullParser.START_DOCUMENT:         
				break;    
			case XmlPullParser.START_TAG:    
				if (Name_TriggerId.equals(pullParser.getName()))
				{
					body.mcpttId = pullParser.nextText();
					body.isLocTrigger = true;
				}
				else if (Name_Latitude.equals(pullParser.getName()))
				{
					String value = pullParser.nextText();
					double val = CheckData.parseDouble(value);
					body.data.setLattitude(val);
				}
				else if (Name_Longitude.equals(pullParser.getName()))
				{
					String value = pullParser.nextText();
					double val = CheckData.parseDouble(value);
					body.data.setLongitude(val);
				}   
				break;

			case XmlPullParser.END_TAG:
				break;

			}

			event = pullParser.next();
		}

		return body;
	}
	
	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
		strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">").append("\r\n");
		strBuff.append("<Report ReportType=\"NonEmergency\">").append("\r\n");
		if(isLocTrigger)
		{
			strBuff.append("<TriggerId>").append(mcpttId).append("</TriggerId>").append("\r\n");
		}
		strBuff.append("<CurrentLocation>").append("\r\n");
		strBuff.append("<CurrentCoordinate>").append("\r\n");
		
		if(data != null)
		{
			strBuff.append("<Longitude type=\"Normal\">").append("\r\n");
			strBuff.append(data.getLongitude());
			strBuff.append("</Longitude>").append("\r\n");
		
			strBuff.append("<Latitude type=\"Normal\">").append("\r\n");
			strBuff.append(data.getLattitude());
			strBuff.append("</Latitude>").append("\r\n");
		}
		
		strBuff.append("</CurrentCoordinate>").append("\r\n");
		strBuff.append("</CurrentLocation>").append("\r\n");
		strBuff.append("</Report>").append("\r\n");
		
		strBuff.append("</location-info>");
		
		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return strBuff.toString();
	}
	
	
	public static String getTestStr(String mcpttId,boolean isLocTrigger,LocationData data)
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">");
		strBuff.append("<Report ReportID=\"xxx\" ReportType=\"Emergency\">");
		if(isLocTrigger)
		{
			strBuff.append("<TriggerId>").append(mcpttId).append("</TriggerId>");
		}
		strBuff.append("<CurrentLocation>");
		strBuff.append("<CurrentCoordinate>");
		
		if(data != null)
		{
			strBuff.append("<Longitude type=\"Normal\">");
			strBuff.append(data.getLongitude());
			strBuff.append("</Longitude>");
		
			strBuff.append("<Latitude type=\"Normal\">");
			strBuff.append(data.getLattitude());
			strBuff.append("</Latitude>");
		}
		
		strBuff.append("</CurrentCoordinate>");
		strBuff.append("</CurrentLocation>");
		strBuff.append("</Report>");
		
		strBuff.append("</location-info>");
		
		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return strBuff.toString();
	}
	
	
	public String getMcpttId() {
		return mcpttId;
	}

	public boolean isLocTrigger() {
		return isLocTrigger;
	}

	public LocationData getData() {
		return data;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
